version: '3.8'
services:
  db:
    image: postgres:14-alpine
    container_name: server-db
    networks:
      - db_server
    ports:
      - '5432:5432'
    restart: unless-stopped
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
      interval: 2s
      timeout: 5s
      retries: 10
    environment:
      POSTGRES_USER: db-user
      POSTGRES_PASSWORD: db-password
      POSTGRES_DB: server
    volumes:
      - pgdata:/var/lib/postgresql/data

  server:
    image: server
    container_name: server-application
    build:
      context: .
      dockerfile: Dockerfile
      args:
        PROJECT_DIRECTORY: /tmp/server
    networks:
      - db_server
    ports:
      - '8080:8080'
    depends_on:
      db:
        condition: service_healthy
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/server
      SPRING_DATASOURCE_USERNAME: db-user
      SPRING_DATASOURCE_PASSWORD: db-password

networks:
  db_server:
    name: db-server-network
  server_client:
    name: db-server-network

volumes:
  pgdata: